﻿using Sunny.UI;
using supermarket.page;
using Newtonsoft.Json;

namespace supermarket
{
    public partial class Formmain : UIForm
    {
        public static user user;
        public Formmain()
        {
            InitializeComponent();
        }

        private void Formmain_Load(object sender, EventArgs e)
        {
            uiLabel1.Text += user.name;
            uiLabel2.Text += user.sex;
            uiLabel3.Text += user.age;
            uiLabel4.Text += user.addr;
            uiLabel5.Text += user.phone;
            //TopLeft
            uiLabel7.TextAlign = ContentAlignment.TopLeft;
            uiButton1_Click(sender, e);
            res res = HTTPUtils.GET("goods/Statistics");
            if (res.cod != "200")
            {
                MessageBox.Show(res.msg, "统计信息错误！", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            Statistics Statistics = JsonConvert.DeserializeObject<Statistics>(res.date.ToString());
            uiLabel8.Text += Statistics.user;
            uiLabel10.Text += Statistics.goods;
            uiLabel9.Text += Statistics.supplier;
        }

        private void menultem(TreeNode node, NavMenuItem item, int pageIndex)
        {
            if (node.Text == "全部员工")
            {
                if (user.admin == "否") { MessageBox.Show("权限不足无法操作", "权限不足", MessageBoxButtons.OK, MessageBoxIcon.Error); return; }else 
                if (!ExistPage(1001))
                {
                    AddPage(new alluser());
                }
                SelectPage(1001);
                return;
            }
            if (node.Text == "查看供应商")
            {
                if (!ExistPage(1002))
                {
                    AddPage(new allsupplier());
                }
                SelectPage(1002);
                return;
            }
            if (node.Text == "商品搜索")
            {
                if (!ExistPage(1003))
                {
                    AddPage(new querygoods());
                }
                SelectPage(1003);
                return;
            }
            if (node.Text == "添加供应商")
            {
                UIEditForm supplierfrm = form.supplierform("添加供应商", new supplier());
                supplierfrm.Render();
                supplierfrm.ShowDialog();
                if (supplierfrm.IsOK)
                {
                    res res = HTTPUtils.jsonPost("supplier/add", new
                    {
                        name = supplierfrm["name"],
                        addr = supplierfrm["addr"],
                        phone = supplierfrm["phone"],
                        goods = supplierfrm["goods"],
                    });
                    if (res.cod != "200")
                    {
                        MessageBox.Show(res.msg, "登记供应商发送错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                    else
                    {
                        MessageBox.Show(res.msg, "登记供应商成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    
                }
                return;
            }

            if (node.Text == "添加员工")
            {
                if (user.admin == "否")
                {
                    MessageBox.Show("权限不足无法操作", "权限不足", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                UIEditForm frm = form.userform("添加店员",new user());
                frm.Render();
                frm.ShowDialog();
                if (frm.IsOK)
                {
                    res res = HTTPUtils.jsonPost("users/add", new {
                        name = frm["name"],
                        paw = frm["paw"],
                        phone = frm["phone"],
                        sex = frm["Sex"].ToString() == "0" ? "男" : "女",
                        age = frm["Age"],
                        addr = frm["addr"],
                        admin = frm["admin"].ToString()=="true" ? "是":"否"
                    });
                    if(res.cod != "200")
                    {
                        MessageBox.Show(res.msg, "登记店员发送错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                    else
                    {
                        MessageBox.Show(res.msg, "登记店员成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
            }
            if (node.Text == "添加商品")
            {
                res res = HTTPUtils.GET("supplier/getallname");
                var date = JsonConvert.DeserializeObject< List<String>>(res.date.ToString());
                UIEditForm frm = form.goodsform("添加商品",new goods(),date.ToArray());
                frm.Render();
                frm.ShowDialog();
                if (frm.IsOK)
                {
                    int i = Convert.ToInt32(frm["suppliers"]);
                    res res1 = HTTPUtils.jsonPost("goods/add", new {
                        name=frm["name"],
                        monovalent= frm["monovalent"],
                        suppliername= date.ToArray()[i]

                    });
                    if (res1.cod != "200")
                    {
                        MessageBox.Show(res1.msg, "商品添加错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                    else
                    {
                        MessageBox.Show(res1.msg, "商品添加成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
            }
            if (node.Text == "商品进货")
            {
                res res = HTTPUtils.GET("purchasecontrol/form");
                var date = JsonConvert.DeserializeObject<Dictionary<string, List<String>>>(res.date.ToString());
                UIEditForm frm = form.purchasecontrolform("商品进货", date["Supplers"].ToArray(),date["goodss"].ToArray());
                frm.Render();
                frm.ShowDialog();
                if (frm.IsOK)
                {
                    if (Convert.ToInt32(frm["mod"]) < 0)
                    {
                        MessageBox.Show("进货不能为负数！", "登记店员发送错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                    int i = Convert.ToInt32(frm["Supplers"]);
                    int j = Convert.ToInt32(frm["goods"]);
                    res res1 = HTTPUtils.jsonPost("purchasecontrol/add", new
                    {
                        username = user.name,
                        Suppler = date["Supplers"].ToArray()[i] ,
                        goods = date["goodss"].ToArray()[j],
                        quantity = frm["mod"],
                    });
                    if (res1.cod != "200")
                    {
                        MessageBox.Show(res1.msg, "商品进货错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                    else
                    {
                        MessageBox.Show(res1.msg, "商品进货成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
            }
            if (node.Text == "进货日志")
            {
                if (!ExistPage(1004))
                {
                    AddPage(new Purchaserecord());
                }
                SelectPage(1004);
                return;
            }
            //1005
            if (node.Text == "出售记录")
            {
                if (!ExistPage(1005))
                {
                    AddPage(new salerecord());
                }
                SelectPage(1005);
                return;
            }
            //退货日志
            if (node.Text == "退货日志")
            {
                if (!ExistPage(1006))
                {
                    AddPage(new goodscancel());
                }
                SelectPage(1006);
                return;
            }
        }

        private void uiButton1_Click(object sender, EventArgs e)
        {
            uiLabel7.Text = "";
            res res = HTTPUtils.jsonPost("goods/earlywarning", new
            {
                i = uiTextBox1.Text.ToString(),
            });

            if (res.cod != "200")
            {
                MessageBox.Show(res.msg, "库存预警错误！", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            List<goods> goodss = JsonConvert.DeserializeObject<List<goods>>(res.date.ToString());
            goodss.ForEach(goods =>
            {
                uiLabel7.Text += goods.name + "库存剩余:" + goods.quantity + "\n";
            });
        }
    }
}
